package com.forum.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.forum.annotation.IgnoreAuth;
import com.forum.entity.OrderrefundEntity;
import com.forum.entity.view.OrderrefundView;
import com.forum.service.OrderrefundService;
import com.forum.utils.MPUtil;
import com.forum.utils.PageUtils;
import com.forum.utils.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 订单退款表 前端控制器
 * </p>
 *
 * @author 张继成
 * @since 2023-12-31
 */
@RestController
@RequestMapping("/dingdantuikuan")
@Api(tags = "订单退款控制")
public class OrderrefundController {
    @Autowired
    private OrderrefundService dingdantuikuanService;

    //后端列表
    @RequestMapping("/page")
    public R page(@RequestParam Map<String,Object> params, OrderrefundEntity dingdantuikuan,
                  HttpServletRequest request){
        String tableName=request.getSession().getAttribute("tableName").toString();
        if (tableName.equals("xuesheng")){
            dingdantuikuan.setStudentnum((String)request.getSession().getAttribute("username"));
        }
        EntityWrapper<OrderrefundEntity> ew=new EntityWrapper<OrderrefundEntity>();
        PageUtils page=dingdantuikuanService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew,dingdantuikuan),params),params));
        return R.ok().put("data",page);
    }

    //前端列表
    @IgnoreAuth
    @RequestMapping("/list")
    @SuppressWarnings("unchecked")
    public R list(@RequestParam Map<String, Object> params,OrderrefundEntity dingdantuikuan,
                  HttpServletRequest request){
        EntityWrapper<OrderrefundEntity> ew = new EntityWrapper<OrderrefundEntity>();

        PageUtils page = dingdantuikuanService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, dingdantuikuan), params), params));
        return R.ok().put("data", page);
    }

    //列表
    @RequestMapping("/lists")
    @SuppressWarnings("unchecked")
    public R list( OrderrefundEntity dingdantuikuan){
        EntityWrapper<OrderrefundEntity> ew = new EntityWrapper<OrderrefundEntity>();
        ew.allEq(MPUtil.allEQMapPre( dingdantuikuan, "dingdantuikuan"));
        return R.ok().put("data", dingdantuikuanService.selectListView(ew));
    }

    //查询
    @RequestMapping("/query")
    @SuppressWarnings("unchecked")
    public R query(OrderrefundEntity dingdantuikuan){
        EntityWrapper <OrderrefundEntity> ew=new EntityWrapper<OrderrefundEntity>();
        ew.allEq(MPUtil.allEQMapPre(dingdantuikuan,"dingdantuikuan"));
        OrderrefundView dingdantuikuanView=dingdantuikuanService.selectView(ew);
        return R.ok("查询订单退款成功").put("data",dingdantuikuanView);
    }

    //后端详情
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        OrderrefundEntity dingdantuikuan=dingdantuikuanService.selectById(id);
        return R.ok().put("data", dingdantuikuan);
    }

    //前端详情
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        OrderrefundEntity dingdantuikuan = dingdantuikuanService.selectById(id);
        return R.ok().put("data", dingdantuikuan);
    }

    //后端保存
    @RequestMapping("/save")
    public R save(@RequestBody OrderrefundEntity dingdantuikuan, HttpServletRequest request){
        dingdantuikuan.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        dingdantuikuanService.insert(dingdantuikuan);
        return R.ok();
    }

    //前端保存
    public R add(@RequestBody OrderrefundEntity dingdantuikuan, HttpServletRequest request){
        dingdantuikuan.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        dingdantuikuanService.insert(dingdantuikuan);
        return R.ok();
    }

    //修改
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody OrderrefundEntity dingdantuikuan, HttpServletRequest request){
        dingdantuikuanService.updateById(dingdantuikuan);//全部更新
        return R.ok();
    }

    //删除
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        dingdantuikuanService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
}
